import { NextRequest } from 'next/server';
import { withMiddleware } from '@/lib/middleware/entry';
import { prisma } from '@/lib/prisma';
import { authenticateRequest } from '@/utils/auth';

async function handler(req: NextRequest) {
    const { id } = await req.json();

    if (!id) {
        throw new Error('Missing id');
    }

    const content = await prisma.content.findUnique({
        where: { id },
        include: {
            categories: {
                include: {
                    category: true,
                },
            },
        },
    });

    if (!content) {
        throw new Error('Content not found');
    }

    await prisma.content.update({
        where: { id },
        data: {
            viewCount: {
                increment: 1,
            },
        },
    });

    return { data: content };
}

export const POST = withMiddleware(handler, { beforeCallback: [authenticateRequest] });